<?php
// +----------------------------------------------------------------------
// | ZanPianCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpiancms.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.zanpiancms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use app\common\library\Letter;
use think\Model;
use think\Cache;
class Mcat extends Model{
   protected function setMEnameAttr($value,$data){
	 if (empty($value)){
		     $Letter = new Letter();
		     $listdir=$Letter->getletters(trim($data['m_name']),'mcat',$data['m_list_id']);
		     return $listdir;
	     }else{
		     return trim($value);
	     }
    }	
	//小分类获取
    public function get_mcid($cid,$mcid){	
		  $pid=getlistname($cid,'list_pid');
		  $condition=!empty($pid) ? $pid : $cid;	
          $tagArr = array();
          $tag = array_unique(explode(',',trim($mcid))); 
		  $where['m_list_id']=$condition;
		  $where['m_name']=array('IN',$tag);
		  $mcid=db('Mcat')->field('m_cid')->where($where)->select();
		  if($mcid){
		    foreach($mcid as $k=>$v){
			    $tagArr[]=$v['m_cid'];  
		    }
		    return implode(',',$tagArr);
          }else{
			return  "";  
		}
	}
	public function listMcid(){
		$mcid=cache('mcid');
		if(empty($mcid)){
		    $mcid = db('Mcat')->order('m_cid asc')->column('m_list_id,m_cid,m_name,m_ename,m_order','m_cid');
		    Cache::tag('mcat')->set('mcid',$mcid);
		}
		return  $mcid;
	}	
	public function delCache(){
		Cache::clear('mcat');
	}
	public function listData($tag=''){
		if(!is_array($tag)) {
            $tag=json_decode($tag, true);
        }
	    if(!empty($tag['cid'])){
	        $pid=getlistname($tag['cid'],'list_pid');
	        $cid=!empty($pid) ? $pid : $tag['cid'];
            $tree = db('Mcat')->where('m_list_id',$cid)->cache(md5($cid),0,'mcat')->order('m_order asc')->select();
			return $tree;
			
	    }else{
			$mcat=cache('mcat');
			if(empty($mcat)){
		        $tree = db('List')->where(['list_pid'=>0,'list_sid'=>1])->cache('list',0,'mcat')->field('list_id,list_name,list_pid,list_dir,list_oid')->order('list_oid asc')->select();
		        foreach ($tree as $k=>$v){
			        $mcat[$v['list_id']]['list_id']=$v['list_id'];
			        $mcat[$v['list_id']]['list_pid']=$v['list_pid'];
			        $mcat[$v['list_id']]['list_name']=$v['list_name'];
			        $mcat[$v['list_id']]['list_dir']=$v['list_dir'];
			        $mcat[$v['list_id']]['list_oid']=$v['list_oid'];
			        if(!empty($v['list_pid'])){
			            $mcat[$v['list_id']]['son'] = db('Mcat')->where('m_list_id',$v['list_pid'])->cache(md5($v['list_pid']),0,'mcat')->order('m_order asc')->select();
			        }else{
				        $mcat[$v['list_id']]['son'] = db('Mcat')->where('m_list_id',$v['list_id'])->cache(md5($v['list_id']),0,'mcat')->order('m_order asc')->select();
			        }
			        $mcat[$v['list_id']]['total'] = $mcat[$v['list_id']]['son'] == null ? 0 : count($mcat[$v['list_id']]['son']);
		        }
				Cache::tag('mcat')->set('mcat',$mcat);
			}
		    return $mcat;
		}		
	}	

}